﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Compression.aspx.cs" Inherits="ASPNETPerformance._01_ViewState.Compression"
    MasterPageFile="~/Site.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

    <script type="text/javascript">
        function GetStateZize(isDefault) {
            var viewStateField;
            if (isDefault == true) {
                viewStateField = $("input[id=__VIEWSTATE][type=hidden]");
            }
            else {
                viewStateField = $("input[id=__ZIPSTATE][type=hidden]");
            }

            if (viewStateField != null) {
                var viewStateLength = viewStateField.val().length;

                var hostName = $(location).attr('host');

                if (hostName.indexOf("localhost") == 0)
                    alert('页面的ViewState大小是, ' + viewStateLength + ' bytes.');
            }
        }
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <h2>
        ViewState压缩</h2>
    <p>
        使用<b>System.IO.Compression</b> 压缩ViewState。<br />
    </p>
    <p>
        <b>System.IO.Compression</b> 命名空间包含提供基本的流压缩和解压缩服务的类。
    </p>
    <p>
        此命名空间包含2个类分别为：</p>
    <ul>
        <li><a href="http://msdn.microsoft.com/zh-cn/library/system.io.compression.deflatestream(v=VS.80).aspx">
            DeflateStream</a> 提供用于使用 Deflate 算法压缩和解压缩流的方法和属性。</li>
        <li><a href="http://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream(v=VS.80).aspx">
            GZipStream</a> 提供用于压缩和解压缩流的方法和属性。 </li>
    </ul>
    <p>
        在下面的演示代码中，我们创建一个ViewStateCompression类，包含2个方法，并都返回byte[]数据:</p>
    <p>
        <strong>1.GZipStream版的压缩/解压缩</strong></p>
    <p>
        <strong>2.执行ViewStateCompression类</strong></p>
    <p>
        想使用ViewStateCompression的压缩和解压页面ViewState的功能，我们必须重写 System.Web.UI.Page 的 SavePageStateToPersistenceMedium()
        和LoadPageStateFromPersistenceMedium() 方法.</p>
    <p>
        SavePageStateToPersistenceMedium 方法 可以反序列化的ViewState，它接受一个 ViewState对象的参数.</p>
    <p>
        LoadPageStateFromPersistenceMedium 方法 可以序列化ViewState，它接受一个Base64编码的字符串参数.</p>
    <p>
        经过上述的方法后，你的ViewState可能会减少30-40％。<asp:Button ID="btnAlert" runat="server" Text="显示ViewState的大小" />
    </p>
    <div class="grid">
        <div class="rounded">
            <div class="top-outer">
                <div class="top-inner">
                    <div class="top">
                        <h2>
                            Northwind Products</h2>
                    </div>
                </div>
            </div>
            <div class="mid-outer">
                <div class="mid-inner">
                    <div class="mid">
                        <!-- Content Goes Here! -->
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="dsProducts"
                            CssClass="datatable" AllowPaging="true" AllowSorting="true" CellPadding="0" CellSpacing="0"
                            BorderWidth="0" GridLines="None" SortedAscendingHeaderStyle-CssClass="sortasc"
                            SortedDescendingHeaderStyle-CssClass="sortdesc" SortedAscendingCellStyle-CssClass="sortasc"
                            SortedDescendingCellStyle-CssClass="sortdesc">
                            <PagerStyle CssClass="pager-row" />
                            <RowStyle CssClass="row" />
                            <PagerSettings Mode="NumericFirstLast" PageButtonCount="7" FirstPageText="«" LastPageText="»" />
                            <Columns>
                                <asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" />
                                <asp:BoundField DataField="CategoryName" HeaderText="Category" SortExpression="CategoryName" />
                                <asp:BoundField DataField="CompanyName" HeaderText="Supplier" SortExpression="CompanyName" />
                                <asp:BoundField DataField="QuantityPerUnit" HeaderText="Qty/Unit" SortExpression="QuantityPerUnit" />
                                <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="Price"
                                    SortExpression="UnitPrice"></asp:BoundField>
                                <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued">
                                </asp:CheckBoxField>
                            </Columns>
                        </asp:GridView>
                        <asp:SqlDataSource ID="dsProducts" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                            SelectCommand="SELECT Products.ProductName, Categories.CategoryName, Suppliers.CompanyName, Products.QuantityPerUnit, Products.UnitPrice, Products.Discontinued FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID ORDER BY Products.ProductName">
                        </asp:SqlDataSource>
                    </div>
                </div>
            </div>
            <div class="bottom-outer">
                <div class="bottom-inner">
                    <div class="bottom">
                    </div>
                </div>
            </div>
        </div>
    </div>
</asp:Content>
